<template>
	<view class="register-container">
		<view class="back-button" @click="goBack">
			<text class="back-icon">←</text>
			<text class="back-text">返回</text>
		</view>
		<text class="title">注册</text>
		
		<view class="input-group">
			<view class="input-wrapper">
				<label for="phone" class="input-label">手机号</label>
				<input 
					id="phone"
					class="input-box" 
					type="text" 
					v-model="phone" 
					placeholder="请输入11位手机号"
					placeholder-class="placeholder-style"
					aria-label="手机号输入框"
				/>
			</view>
			
			<view class="verify-code-container">
				<view class="input-wrapper verify-wrapper">
					<label for="verifyCode" class="input-label">验证码</label>
					<input 
						id="verifyCode"
						class="input-box verify-input" 
						type="text" 
						v-model="verifyCode" 
						placeholder="请输入验证码"
						placeholder-class="placeholder-style"
						aria-label="验证码输入框"
					/>
				</view>
				<button 
					class="verify-btn" 
					:disabled="!isPhoneValid || cooldown > 0"
					@click="sendVerifyCode"
					aria-label="发送验证码按钮"
				>
					{{ cooldown > 0 ? `已发送(${cooldown}s)` : '发送验证码' }}
				</button>
			</view>
			
			<view class="input-wrapper">
				<label for="password" class="input-label">密码</label>
				<input 
					id="password"
					class="input-box" 
					type="password" 
					v-model="password" 
					placeholder="请设置6位以上密码"
					placeholder-class="placeholder-style"
					aria-label="密码输入框"
				/>
			</view>
			
			<button 
				class="register-btn" 
				@click="handleRegister" 
				:disabled="!isFormValid"
				aria-label="注册按钮"
			>
				同意协议并注册
			</button>
		</view>
		
		<view class="agreement">
			<text class="agreement-text" @click="showAgreement" role="button" aria-label="查看注册协议">注册协议>></text>
		</view>
		
		<view class="login-link">
			<text>已注册账号？</text>
			<text class="login-text" @click="goToLogin" role="button" aria-label="前往登录页面">去登录</text>
		</view>
	</view>
</template>

<script>
	import dbService from '../../utils/database.js';
	
	export default {
		data() {
			return {
				phone: '',
				verifyCode: '',
				password: '',
				cooldown: 0,
				timer: null,
				expectedCode: '' // 用于存储生成的验证码
			}
		},
		computed: {
			isPhoneValid() {
				return /^1[3-9]\d{9}$/.test(this.phone);
			},
			isFormValid() {
				return this.isPhoneValid && 
					   this.verifyCode === this.expectedCode && 
					   this.password.length >= 6;
			}
		},
		methods: {
			goBack() {
				uni.navigateBack();
			},
			goToLogin() {
				uni.redirectTo({
					url: '/pages/login/login'
				});
			},
			sendVerifyCode() {
				if (!this.phone || !this.isPhoneValid || this.cooldown > 0) return;
				
				// 生成6位随机验证码
				this.expectedCode = Math.floor(100000 + Math.random() * 900000).toString();
				
				// 在实际应用中，这里应该调用短信服务发送验证码
				uni.showToast({
					title: `验证码：${this.expectedCode}`,
					icon: 'none',
					duration: 3000
				});
				
				this.cooldown = 60;
				this.timer = setInterval(() => {
					this.cooldown--;
					if (this.cooldown <= 0) {
						clearInterval(this.timer);
					}
				}, 1000);
			},
			handleRegister() {
				// 验证表单
				if (!this.phone || !this.password || !this.verifyCode) {
					uni.showToast({
						title: '请填写完整信息',
						icon: 'none'
					});
					return;
				}

				try {
					// 获取已存储的用户数据
					const usersStr = uni.getStorageSync('users');
					const users = usersStr ? JSON.parse(usersStr) : [];
					
					// 检查是否为数组
					if (!Array.isArray(users)) {
						throw new Error('用户数据格式错误');
					}
					
					// 检查手机号是否已注册
					if (users.some(u => u.phone === this.phone)) {
						uni.showToast({
							title: '该手机号已注册',
							icon: 'none'
						});
						return;
					}
					
					// 创建新用户
					const newUser = {
						phone: this.phone,
						password: this.password,
						createTime: new Date().getTime()
					};
					
					// 添加新用户
					users.push(newUser);
					
					// 保存用户数据
					uni.setStorageSync('users', JSON.stringify(users));
					
					// 保存当前用户信息
					uni.setStorageSync('currentUser', JSON.stringify(newUser));
					
					uni.showToast({
						title: '注册成功',
						icon: 'success'
					});
					
					// 使用switchTab跳转到主页
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/home/home'
						});
					}, 1500);
				} catch (error) {
					console.error('注册错误:', error);
					uni.showToast({
						title: '注册失败，请重试',
						icon: 'none'
					});
				}
			},
			showAgreement() {
				uni.showToast({
					title: '用户协议',
					icon: 'none'
				});
			}
		},
		beforeDestroy() {
			if (this.timer) {
				clearInterval(this.timer);
			}
		}
	}
</script>

<style>
	.register-container {
		padding: 60rpx 40rpx;
		min-height: 100vh;
		background-color: #FFFFFF;
		position: relative;
	}
	
	.back-button {
		position: absolute;
		top: 60rpx;
		left: 40rpx;
		display: flex;
		align-items: center;
		z-index: 1;
	}
	
	.back-icon {
		font-size: 40rpx;
		color: #333333;
		margin-right: 10rpx;
	}
	
	.back-text {
		font-size: 28rpx;
		color: #333333;
	}
	
	.title {
		font-size: 48rpx;
		font-weight: bold;
		margin-bottom: 80rpx;
		display: block;
		margin-top: 80rpx;
	}
	
	.input-group {
		margin-top: 40rpx;
	}
	
	.input-wrapper {
		margin-bottom: 30rpx;
		flex: 1;
	}
	
	.input-label {
		display: block;
		font-size: 28rpx;
		color: #333333;
		margin-bottom: 10rpx;
		margin-left: 10rpx;
	}
	
	.verify-wrapper {
		margin-right: 20rpx;
		margin-bottom: 0;
	}
	
	.input-box {
		width: 100%;
		height: 100rpx;
		background: #FFFFFF;
		border-radius: 16rpx;
		padding: 0 30rpx;
		font-size: 32rpx;
		margin-bottom: 30rpx;
		box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08);
		box-sizing: border-box;
	}
	
	.verify-code-container {
		display: flex;
		align-items: center;
		gap: 20rpx;
		margin-bottom: 30rpx;
	}
	
	.verify-input {
		flex: 1;
		margin-bottom: 0;
	}
	
	.verify-btn {
		width: 200rpx;
		height: 100rpx;
		line-height: 100rpx;
		background-color: #2196F3;
		color: #FFFFFF;
		font-size: 28rpx;
		border-radius: 16rpx;
		padding: 0;
		margin: 0;
		white-space: nowrap;
	}
	
	.verify-btn[disabled] {
		opacity: 0.6;
		background-color: #2196F3;
	}
	
	.placeholder-style {
		color: #999999;
	}
	
	.register-btn {
		width: 100%;
		height: 90rpx;
		line-height: 90rpx;
		background-color: #FF6B00;
		color: #FFFFFF;
		border-radius: 45rpx;
		font-size: 32rpx;
		margin-top: 40rpx;
		border: none;
	}
	
	.register-btn[disabled] {
		opacity: 0.6;
		background-color: #FF6B00;
	}
	
	.agreement {
		text-align: center;
		margin-top: 30rpx;
	}
	
	.agreement-text {
		font-size: 28rpx;
		color: #999999;
	}
	
	.login-link {
		position: fixed;
		bottom: 60rpx;
		left: 0;
		right: 0;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 28rpx;
		color: #333333;
	}
	
	.login-text {
		color: #FF6B00;
		margin-left: 10rpx;
	}
</style> 